//
// AnalyzerTests.cs
//
// Author:
//       Matt Ward <matt.ward@microsoft.com>
//
// Copyright (c) 2020 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using MonoDevelop.PackageManagement.Tests.Helpers;
using MonoDevelop.Projects;
using NUnit.Framework;
using UnitTests;

namespace MonoDevelop.PackageManagement.Tests
{
	[TestFixture]
	public class AnalyzerTests : RestoreTestBase
	{
		/// <summary>
		/// Tests that SDK and non-SDK project with PackageReferences resolves the Analyzers used by the project.
		/// </summary>
		[Test]
		public async Task StyleCopAnalyzer ()
		{
			string solutionFileName = Util.GetSampleProject ("StyleCopAnalyzer", "StyleCopAnalyzer.sln");
			solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName);
			CreateNuGetConfigFile (solution.BaseDirectory);
			var netStandardProject = (DotNetProject)solution.FindProjectByName ("NetStandard");
			var packageReferenceProject = (DotNetProject)solution.FindProjectByName ("NetFramework");

			var process = Process.Start ("msbuild", $"/t:Restore /p:RestoreDisableParallel=true \"{solutionFileName}\"");
			Assert.IsTrue (process.WaitForExit (120000), "Timeout restoring NuGet packages.");
			Assert.AreEqual (0, process.ExitCode);

			var netStandardProjectAnalyzers = await netStandardProject.GetAnalyzerFilesAsync (ConfigurationSelector.Default);
			var packageReferenceProjectAnalyzers = await packageReferenceProject.GetAnalyzerFilesAsync (ConfigurationSelector.Default);

			var netStandardProjectAnalyzerFileNames = netStandardProjectAnalyzers.Select (f => f.FileName).ToArray ();
			var packageReferenceProjectAnalyzerFileNames = packageReferenceProjectAnalyzers.Select (f => f.FileName).ToArray ();

			Assert.That (packageReferenceProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.dll"));
			Assert.That (packageReferenceProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.CodeFixes.dll"));
			Assert.That (netStandardProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.dll"));
			Assert.That (netStandardProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.CodeFixes.dll"));
		}
	}
}
